package org.erepublik.company.scanner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.erepublik.company.ECountry;
import org.erepublik.company.EProductType;
import org.erepublik.company.comparators.HouseComparatorByWellness;
import org.erepublik.company.product.EProductAttribute;
import org.erepublik.company.product.House;
import org.erepublik.company.product.ProductDesign;
import org.erepublik.company.product.ProductKey;
import org.erepublik.company.service.ICompanyCountryService;
import org.erepublik.company.service.IVatService;
import org.erepublik.company.service.JSONExchangeRateService;

public class HousesScanner extends ProductScanner<House> {

    @Override
    protected House[] getEmptyTable() {
        return new House[0];
    }
    @Override
    protected House getNewProduct() {
        return new House();
    }
    @Override
    public House[] getProductsFromCountry(ECountry country, IVatService vatService, ICompanyCountryService companyService,
            JSONExchangeRateService currency) throws Exception {
        ProductDesign productDesign = new ProductDesign(EProductType.HOUSE);
        HashMap<ProductKey, House> map = new HashMap<ProductKey, House>();

        ArrayList<CutValue> cutValue = new ArrayList<CutValue>();
        ArrayList<CutValue> topCutValue = new ArrayList<CutValue>();
        for (int i = 0; i <= 100; i += 10) {
            for (int j = 0; j <= 100; j += 10) {
                if (!isWorthChecking(i, j, cutValue, topCutValue))
                    continue;
                productDesign.setAttributeValue(EProductAttribute.WELLNESS, i);
                productDesign.setAttributeValue(EProductAttribute.DURABILITY, j);
                House[] scannedProducts = getProductList(country, productDesign, vatService, companyService, currency);
                CutValue topCut = getMinimalAttributes(scannedProducts);
                topCutValue.add(topCut);
                
                if (scannedProducts.length < 10)
                    cutValue.add(new CutValue(i, j));
                
 
                for (House product : scannedProducts) {
                    ProductKey key = new ProductKey(product.getCompanyOwner().getId(), country);
                    if (!map.containsKey(product)) {
                        map.put(key, product);
                    }
                }
            }
        }

        House[] products = map.values().toArray(new House[0]);
        sortProductByEfficency(products);
        return products;
    }


    private boolean isWorthChecking(int i, int j, ArrayList<CutValue> cutValues, ArrayList<CutValue> topCutValue) {
        for (CutValue cutValue : cutValues) {
            if (j >= cutValue.getHeppinessCutValue() && i >= cutValue.getWellnessCutValue())
                return false;
        }
        for (CutValue cutValue : topCutValue) {
            if (j <= cutValue.getHeppinessCutValue() && i <= cutValue.getWellnessCutValue()) {
                return false;
            }
        }
        return true;
    }



    private CutValue getMinimalAttributes(House[] scannedProducts) {
        int minWell=100;
        int minHapp=100;
        for (int i=0; i < scannedProducts.length; i++) {
            if (minWell > scannedProducts[i].getProductDesign().getAttributeValue(EProductAttribute.WELLNESS))
                minWell = scannedProducts[i].getProductDesign().getAttributeValue(EProductAttribute.WELLNESS);
            if (minHapp > scannedProducts[i].getProductDesign().getAttributeValue(EProductAttribute.HAPPINESS))
                minHapp = scannedProducts[i].getProductDesign().getAttributeValue(EProductAttribute.HAPPINESS);
        }
        return new CutValue(minWell, minHapp);
    }
    

    private void sortProductByEfficency(House[] products) {
        Arrays.sort(products, new HouseComparatorByWellness());
    }


    
}
